﻿package com.gm.tools;

import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class TrainParse {
	Document doc;

	public TrainParse(String html) {
		this.doc = Jsoup.parse(html);
	}
	
	public String[] parseTrainNumber() {
		Element table = doc.select("#checi").get(0);
		Elements singles = table.select(".single");
		String[] data = new String[singles.size()];
		for (int i = 0; i < singles.size(); i++) {
			Elements items = singles.get(i).select("i");
			StringBuffer sbf = new StringBuffer();
			for (int j = 0; j < 10; j++) {
				switch (j) {
				case 0:
					sbf.append(items.get(j).text() + "\t\n" + "站名:");
					break;
				case 1:
					sbf.append(items.get(j).text() + "到达时间:");
					break;
				case 2:
					sbf.append(items.get(j).text() + "\t\n" + "出发时间:");
					break;
				case 3:
					sbf.append(items.get(j).text() + "  停留:");
					break;
				case 4:
					sbf.append(items.get(j).text() + "\t\n" + "运行时间:");
					break;
				case 5:
					sbf.append(items.get(j).text() + "  里程:");
					break;
				case 6:
					sbf.append(items.get(j).text() + "\t\n" + "硬座/软座:");
					break;
				case 7:
					sbf.append(items.get(j).text() +"\t\n"+ "硬座(上/中/下):");
					break;
				case 8:
					sbf.append(items.get(j).text() + "\t\n" + "软座(上/下):");
					break;
				case 9:
					sbf.append(items.get(j).text());
					break;
				default:
				}
			}
			data[i] = sbf.toString();
		}
		return data;
	}

	public String[] parseStation() {
		Element table = doc.select("#checi").get(0);
		Elements singles = table.select(".single");
		String[] data = new String[singles.size()];
		for (int i = 0; i < singles.size(); i++) {
			Elements all_i = singles.get(i).select("i");
			StringBuffer sb = new StringBuffer();
			sb.append(all_i.get(0).select("em").get(0).text() + " | ").append(
					all_i.get(0).select("em").get(1).text()+"\t\n"+ "发站-到站:");
			for (int j = 1; j < all_i.size()-1; j++) {
				switch (j) {
				case 1:
					sb.append(all_i.get(j).text()+" 发车:");
					break;
				case 2:
					sb.append(all_i.get(j).text() + "\t\n" + "到达:");
					break;
				case 3:
					sb.append(all_i.get(j).text() + " 运行时间:");
					break;
				case 4:
					sb.append(all_i.get(j).text() + "\t\n" + "里程:");
					break;
				case 5:
					sb.append(all_i.get(j).text() + "\t\n"+" 参考标价(元):");
					break;
				case 6:
					sb.append(all_i.get(j).text());
					break;
				}
			}
			data[i] = sb.toString();
		}
		return data;
	}
}